package com.example.cloud.model.system;

import com.example.cloud.model.common.CommonModel;
import lombok.Data;
import lombok.EqualsAndHashCode;
import org.hibernate.annotations.Fetch;
import org.hibernate.annotations.FetchMode;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by wangtao on 2018/1/26.
 * 用户信息表
 */
@Entity(name = "sys_user")
@Data
@EqualsAndHashCode(callSuper = false)
public class User extends CommonModel{
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;
    // 用户名
    @Column(name = "username")
    private String username;
    // 登录用户名
    @Column(name = "user_login_name")
    private String userLoginName;
    // 登录密码
    @Column(name = "password")
    private String password;
    // 岗位
    @Column(name = "job")
    private Integer job;
    // 部门编号
    @Column(name = "department_id")
    private Long departmentId;
    // 部门名称
    @Column(name = "department_name")
    private String departmentName;
    // 是否管理员 0：不是 1：是
    @Column(name = "is_admin",columnDefinition = "INT default 0")
    private int isAdmin;
    // 是否后勤管理{0:否1： 是}
    @Column(name = "is_logistics", columnDefinition = "INT DEFAULT 0")
    private int isLogistics;
    // 是否专家{0:否 1：是}
    @Column(name = "is_expert", columnDefinition = "INT DEFAULT 0")
    private int isExpert;
    // 用户状态 {0：激活 1：冻结}
    @Column(name = "status", columnDefinition = "INT DEFAULT 0")
    private int status;
    // 签名
    @Column(name = "sign")
    private String sign;
    // 头像
    @Column(name = "avatar",length = 2000)
    private String avatar;
    // 背景
    @Column(name = "skin")
    private String skin;
    // 关联角色
    @ManyToMany(fetch = FetchType.EAGER)
    @Fetch(value= FetchMode.SUBSELECT)
    @JoinTable(name="user_role",// 指定中间表名称
        joinColumns = {@JoinColumn(name="user_id")},// 本表与中间表关联字段
        inverseJoinColumns = {@JoinColumn(name = "role_id")}) // 指定映射表主键
    private List<Role> roles = new ArrayList<Role>();
    // 关联组织机构
    @ManyToMany(fetch = FetchType.EAGER)
    @Fetch(value=FetchMode.SUBSELECT)
    @JoinTable(name="user_organ",// 指定中间表名称
            joinColumns = {@JoinColumn(name="user_id")},// 本表与中间表关联字段
            inverseJoinColumns = {@JoinColumn(name = "organ_id")}) // 指定映射表主键
    private List<Organ> organs = new ArrayList<Organ>();
    // 账户编号
    @Column(name = "account_id")
    private Long accountId;
    // 账户编号
    @Column(name = "account_name")
    private String accountName;
    // 账户所属组织机构
    @Column(name = "organ_id")
    private Long organId;
    // 账户所属组织名称
    @Column(name = "organ_name")
    private String organName;
}
